import requests
import json
from bs4 import BeautifulSoup
import datetime
from loaddata.mgDB import JinCaiDatabase,Wan500Database
from loaddata import load_ouzhi
import urllib3
from tqdm import tqdm
urllib3.disable_warnings()
'''
下载竞猜数据
'''
class LoadDataJinCai:
    def __init__(self):
        self.mgdb = JinCaiDatabase()
    '''
    爬取数据
    '''
    def gethtml(self,url):
        headers = {
            "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36"
        }
        # r = requests.get(url=url, params=params, headers=headers)
        res = requests.get(url, verify=False,headers=headers)
        res.encoding = "utf-8"
        return res.text

    # 获取开奖详情
    def getfixedBonus(self,matchId):
        url = "https://webapi.sporttery.cn/gateway/jc/football/getFixedBonusV1.qry?clientCode=3001&matchId={}" .format(
            matchId)
        data = self.gethtml(url)
        res = json.loads(data)
        return res.get("value")

    # 获取每30天的数据
    def getdatas(self,s,e):
        print("正在下载", s,"到",e, "的数据")
        url = "https://webapi.sporttery.cn/gateway/jc/football/getMatchResultV1.qry?" \
              "matchPage=1&matchBeginDate={}&matchEndDate={}&leagueId=&pageSize=10000&pageNo=1&isFix=0&pcOrWap=0".format(
            s, e)
        data = self.gethtml(url)
        result = json.loads(data)
        matchResultList = result.get("value").get("matchResult")
        rs = []
        for m in tqdm(matchResultList):
            matchDate = m.get("matchDate")
            matchNum = m.get("matchNum")
            no = int(matchNum)//1000
            mdate = datetime.datetime.strptime(matchDate, '%Y-%m-%d')
            # 将日期转换为星期对应的数字（0表示周一）
            weekday_number = mdate.weekday() + 1
            # 修改开奖日期及比赛，保证竞猜日期与场次一致，方便查看
            if weekday_number != no:
                mdate -= datetime.timedelta(days=1)
                date_str = mdate.strftime("%Y-%m-%d")
                m["matchTime"] = date_str
            else:
                m["matchTime"] = matchDate
            # 添加一个场次唯一（场次日期+场次编码）
            m["matchNo"] = f"{m.get('matchTime')}{matchNum}"

            flag = self.mgdb.get_jincai_config({"key": "updata_flag"})
            # 判断是否已经下载,已经存在，就不添加了
            sf = flag.get("setValue").get(m.get("matchNo"))
            if sf:
                continue
            else:
                m["fixedBonus"] = self.getfixedBonus(m.get("matchId"))
                rs.append(m)
        return rs


    # 保持到数据库
    def save(self,ds):
        self.mgdb.batch_sava_to_jincai(ds)


    # 竞猜网下载受关注的比赛（正在售卖的场次）
    def loadSelling(self):
        # 先判断一下是不是5分钟前刚刚下载，如果下载了，就不用，间隔5分钟之后跟新一场
        re_selling = self.mgdb.get_selling({})
        # 获取当前时间
        now = datetime.datetime.now().timestamp()
        # flag = False
        # if re_selling:
        #     if (re_selling[0].get("updateTime")):
        #         tem = 5 * 60
        #         c = now - re_selling[0].get("updateTime")
        #         if c > tem:
        #             flag = True
        #     else:
        #         flag = True
        # else:
        #     flag = True
        #
        # if flag:
            # 需要更新一下

        url = "https://webapi.sporttery.cn/gateway/jc/football/getMatchListV1.qry?clientCode=3001"
        data = self.gethtml(url)
        res = json.loads(data)
        # 获取所有的日期比赛
        matchInfoList = res.get("value").get("matchInfoList")
        # 每天的比赛
        for i in tqdm(range(len(matchInfoList))):
            subMatchList = matchInfoList[i].get("subMatchList")
            for j in range(len(subMatchList)):
                res.get("value").get("matchInfoList")[i].get("subMatchList")[j]["fixedBonus"] = self.getfixedBonus(
                    subMatchList[j].get("matchId"))
        res["updateTime"] = now
        self.mgdb.savaOrUpdataSelling(res)
        # 顺便吧没有更新的比赛结果也更新了
        self.loadMatchScore()


    # 获取开奖比分（上半场，全场）
    def loadMatchScore(self):
        sels = self.mgdb.findMatchs({"info.iskai":0})
        for s in sels:
            url = "https://webapi.sporttery.cn/gateway/uniform/fb/getMatchScoreV1.qry?matchId={}".format(s.get("info").get("matchId"))
            data = self.gethtml(url)
            res = json.loads(data)
            vs = res.get("value").get("sectionsNos")
            if vs:
               self.mgdb.savaOrUpdataMatchScore(res.get("value"))


    '''
    开始时间
    结束时间
    '''
    def moniter(self,start,end):
        datestart = datetime.datetime.strptime(start, '%Y-%m-%d')
        dateend = datetime.datetime.strptime(end, '%Y-%m-%d')

        while datestart < dateend:
            temp = datestart
            temp += datetime.timedelta(days=30)
            # 获取当前时间
            now = datetime.datetime.now()
            # 计算昨天的日期
            yesterday = now
            # yesterday = now - datetime.timedelta(days=1)
            f = False
            # 如果结束日期超过昨天，那么结束日期就是昨天（防止获取没有开奖的信息）
            if temp > yesterday:
                temp = yesterday
                f = True
            # 下载数据兵解析(一天的数据，有很多场比赛)
            datas = self.getdatas(datestart, temp)
            # 保持到数据库
            self.save(datas)
            # 继续下个30天
            datestart += datetime.timedelta(days=31)
            # 更新到今天为止
            if f:
                break




'''
下载500wan数据
'''
class LoadData500wan:
    def __init__(self,t):
        self.mgdb =Wan500Database(t)
    '''
    爬取数据
    '''
    def gethtml(self,url):
        headers = {
            'User-Agent': 'my-app/0.0.1',
            'Authorization': 'Bearer YourAPIToken'
        }
        res = requests.get(url,headers=headers)
        res.encoding = "gbk"
        return res.text



    def getDatas(self,s):
        print("正在下载", s,"的数据")
        url = "https://live.500.com/?e={}".format(s)
        html = self.gethtml(url)
        self.beautiful(html)

    # 解析每天的比赛列表
    def beautiful(self,html):
        bs = BeautifulSoup(html,"html.parser")
        table_match = bs.find("table",attrs={"id":"table_match"})
        trs = table_match.find("tbody").find_all("tr")
        # 如果是空，说明当天没有比赛
        mlist = {}
        if trs:
            for tr in tqdm(trs):
                # 获取tr属性信息
                fid = tr.get("fid")
                # 当天的比赛场次编号
                order = tr.get("order")
                gy = tr.get("gy")
                yy = tr.get("yy")
                living = tr.get("living")
                status = tr.get("status")

                tds = tr.find_all("td")
                lunci = tds[2].text  # 轮次
                pktime = tds[3] .text    # 比赛时间

                zhupm = tds[5].find_all("span")[0].text  # 主队排名
                zhuname = tds[5].find("a").text  # 主队名称
                rangqiu = tds[5].find_all("span")[-1].text  # 让球


                kepm = tds[7].find_all("span")[-1].text  # 客队排名
                kename = tds[7].find("a").text  # 客队名称

                # 获取百家欧指
                ouzhi = self.getouzhi(fid)
                # 获取亚盘比对
                yazhi = self.getyazhi(fid)
                # 获取进球大小指数
                daxiao = self.getdaxiao(fid)
                # 获取投注分析
                touzhu = self.gettouzhu(fid)
                # 获取数据分析
                shuju = self.getshuju(fid)

                m = {"fid": fid, "order": order, "gy": gy, "yy": yy, "living": living, "status": status, "lunci": lunci,
                     "pktime": pktime, "zhupm": zhupm, "zhuname": zhuname, "rangqiu": rangqiu,"kepm":kepm,"kename":kename,
                     "ouzhi":ouzhi,"yazhi":yazhi,"daxiao":daxiao,"touzhu":touzhu,"shuju":shuju}
                mlist[order] = m

    #   获取百家欧指
    def getouzhi(self,fid):
        print("正在下载", fid, "的百家欧指数据")
        url = "https://odds.500.com/fenxi/ouzhi-{}.shtml".format(fid)
        html = self.gethtml(url)
        bs = BeautifulSoup(html, "html.parser")
        o =load_ouzhi.getouzhi(bs)
        return o

    #   获取亚盘比对
    def getyazhi(self,fid):
        url = "https://odds.500.com/fenxi/yazhi-{}.shtml".format(fid)
        yazhi = self.gethtml(url)


    #   获取进球大小指数
    def getdaxiao(self,fid):
        url = "https://odds.500.com/fenxi/daxiao-{}.shtml".format(fid)
        daxiao = self.gethtml(url)


    #   获取投注分析
    def gettouzhu(self,fid):
        url = "https://odds.500.com/fenxi/touzhu-{}.shtml".format(fid)
        touzhu = self.gethtml(url)

    #   获取数据分析
    def getshuju(self,fid):
        url = "https://odds.500.com/fenxi/shuju-{}.shtml".format(fid)
        shuju = self.gethtml(url)



    '''
    开始时间
    结束时间
    '''
    def moniter(self,start,end):
        datestart = datetime.datetime.strptime(start, "%Y-%m-%d")
        dateend = datetime.datetime.strptime(end, "%Y-%m-%d")

        while datestart < dateend:
            # 获取当前时间
            now = datetime.datetime.now()
            # 计算昨天的日期
            yesterday = now
            f = False
            # 如果结束日期超过昨天，那么结束日期就是昨天（防止获取没有开奖的信息）
            if datestart > yesterday:
                f = True
            # 下载数据兵解析(一天的数据，有很多场比赛)
            datas = self.getDatas(datestart.strftime('%Y-%m-%d'))
            # 保持到数据库
            # self.save(datas)
            # 继续下个30天
            datestart += datetime.timedelta(days=1)
            # 更新到今天为止
            if f:
                break


# 跟新数据
def load():
    jincai = LoadDataJinCai()
    # 最早的竞猜记录时间2014-10-22
    start = '2024-07-01'
    end = '2024-12-01'
    jincai.moniter(start, end)

# 跟新数据
def loadByTime(start, end):
    jincai = LoadDataJinCai()
    # 最早的竞猜记录时间2014-10-22
    jincai.moniter(start, end)



# 跟新数据
def loadSelling():
    jincai = LoadDataJinCai()
    jincai.loadSelling()

if __name__ == '__main__':
    for t in ['wudi']:
        # LoadData500wan,LoadDataJinCai
        jincai = LoadDataJinCai()
        # 最早的竞猜记录时间2014-10-22
        start = '2024-06-23'
        end = '2024-12-01'
        rs = jincai.moniter(start,end)

    # 正在下载
    # 2023 - 07 - 22
    # 00: 00:00
    # 到
    # 2023 - 0
    # 8 - 21
    # 00: 00:00
    # 的数据